package fr.ece.ing4.si.singlethread;

import java.io.BufferedReader;
import java.nio.*;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import fr.ece.ing4.si.gui.*;

import fr.ece.ing4.si.gui.Window;

public class MonteCarloSimulation {
	
		
		public double MonteCarloStandardOption( String CallPutFlag,double s, double x,double t,double r,double b,double v,int nSteps,int nSimulations)
		{
		
			
			double dt;
			double st;
			double sum = 0;
			double drift;
			double vSqrdt;
			int i;
			int j;
			int z = 0;
			Random ra=new Random();
			long startTime;
			long endTime;
			
			startTime=System.currentTimeMillis();
			dt=t/nSteps;
			drift=(b-((v*v)/2))*dt;
			vSqrdt=v*Math.sqrt(dt);
			
			if(CallPutFlag.equals("c"))
			{
				z=1;
			}
			
			else if(CallPutFlag.equals("p")) 
			{
				z=-1;
			}
			
			for(i=1;i<=nSimulations;i++)
			{
				st=s;
				for(j=1;j<=nSteps;j++)
				{
					st=st*Math.exp(drift+vSqrdt * ra.nextGaussian());
				}
					sum=sum+Math.max(z*(st-x),0);
				}
				
			endTime=System.currentTimeMillis();
			
			System.out.println("Execution time= " + (endTime-startTime));
			return (Math.exp(-r*t)*(sum/nSimulations));
		}	
		
		
		//Singlethread MonteCarlo algorithm
		public double SingleMonteCarlo2(String CallPutFlag,double s, double x,double t,double r,double b,double v,int nSteps,int nSimulations) throws IOException
		{
						
			String info=null;
			String [] data=null;
			double premium=0;
			Window win=new Window();
			
				
			
				final BufferedReader reader =  new BufferedReader(new FileReader("simulation.txt"));
			
				//System.out.println(" comboset="+win.getComboSet());
				info=reader.readLine();
				data=info.split(",");
				premium=MonteCarloStandardOption(CallPutFlag,s,x,t,r,b,v,nSteps,nSimulations);
			
			
		
			
			return premium;
		
		}
		
		public double SingleMonteCarlo() throws IOException
		{
						
			String info=null;
			String [] data=null;
			double premium=0;
			Window win=new Window();
			
				
			
				final BufferedReader reader =  new BufferedReader(new FileReader("simulation.txt"));
			
				//System.out.println(" comboset="+win.getComboSet());
				info=reader.readLine();
				data=info.split(",");
				premium=MonteCarloStandardOption(data[0],Double.valueOf(data[1]),Double.valueOf(data[2]),Double.valueOf(data[3]),Double.valueOf(data[4]),Double.valueOf(data[5]),Double.valueOf(data[6]),Integer.valueOf(data[7]),Integer.valueOf(data[8]));
			
			
		
			
			return premium;
		
		}
		
		
		public static void main(String [] args)
		{
			javax.swing.SwingUtilities.invokeLater(new Runnable()
			{
				public void run()
				{
					Window w = null;
					try {
						w = new Window();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					w.setVisible(true);
					
					
					MonteCarloSimulation m=new MonteCarloSimulation();
					try {
						
						w.setLabel(String.valueOf(m.SingleMonteCarlo()));
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
	
		});
			
				
	}
}
		
		
		





